﻿using BinanceExchange.API.Enums;
using BinanceExchange.API.Models.Request;
using BinanceExchange.API.Models.Response;
using QntPlatform.Common;
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace QntPlatform.BinanceApi
{

   public class FApiClent
    {
        string baseUrl = "https://fapi.binance.com";
        public void GetRecords(int period, int limit = 30) {
            var rc = new RestClient(baseUrl);
 
            var req = new RestRequest("/fapi/v1/klines");
            //req.AddParameter("symbol", "BTCUSDT");
            //req.AddParameter("interval", "1M");
            //req.AddParameter("limit", limit);
           var props= new GetKlinesCandlesticksRequest() { Symbol = "BTCUSDT", Interval = periodConvert(period), Limit = limit };
            req.AddObject(props);
             
            var resp= rc.Get<List<KlineCandleStickResponse>>(req);
            Console.WriteLine(resp);
            
            //key,签名，时间戳

             
        }
        /// <summary>
        /// @/fapi/v1/time
        /// </summary>
        /// <returns></returns>
        public DateTimeOffset GetTime()
        {
            var rc = new RestClient(baseUrl);
            var req = new RestRequest("/fapi/v1/time");
            var resp = rc.Get<DateTimeOffset>(req);//是对象，需改动 https://fapi.binance.com/fapi/v1/time
            return resp.Data;
        }
       public static string serviceUrl;
        public static string GetUserData(string key)
        {
            var rc = new RestClient(serviceUrl);
            var req = new RestRequest("/uapi/UserData/GetUserData");
            req.AddParameter("key", key);
            var resp = rc.Get<Result<string>>(req);
            if (resp.ResponseStatus == ResponseStatus.Completed)
            {
                return resp.Data.Data;
            }
            return null;
        }

        public KlineInterval periodConvert(int period)
        {
            switch (period)
            {
                case 60:
                    return KlineInterval.OneMinute;
                case 3 * 60:
                    return KlineInterval.ThreeMinutes;
                case 5 * 60:
                    return KlineInterval.FiveMinutes;
                case 15 * 60:
                    return KlineInterval.FifteenMinutes;
                case 30 * 60:
                    return KlineInterval.ThirtyMinutes;
                case 3600: return KlineInterval.OneHour;
                case 2 * 3600: return KlineInterval.TwoHours;
                case 4 * 3600: return KlineInterval.FourHours;
                case 6 * 3600: return KlineInterval.SixHours;
                case 8 * 3600: return KlineInterval.EightHours;
                case 12 * 3600: return KlineInterval.TwelveHours;
                case 24 * 3600: return KlineInterval.OneDay;
                case 3 * 24 * 3600: return KlineInterval.ThreeDays;
                case 7 * 24 * 3600: return KlineInterval.OneWeek;
                case 30 * 24 * 3600: return KlineInterval.OneMonth;
                default:
                    throw new ArgumentException();
            }
        }
    }

   }
